package ca.concordia.comp6231.client;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.Scanner;

import ca.concordia.comp6231.server.StationServerInterface;

public class OfficerClient {

	public static void main(String[] args) {
		displayWelcome();
		String badgeID = getBadgeID();
		String stationName = extractStationNameFromBadgeID(badgeID);
		
		OfficerClientConfiguration config = new OfficerClientConfiguration(stationName);
		String connection = config.getServerConnectionString();
		StationServerInterface server = loadServer(connection);
		
		try{
			server.createCRecord("test", "test", "tset", "test");
			server.createCRecord("test", "test", "tset", "test");
			
			server.createCRecord("test", "Aest", "tset", "test");
			server.createCRecord("test", "Xest", "tset", "test");
			
			server.createCRecord("test", "Test", "tset", "test");
			
			server.createMRecord("test", "Test", "tset", "test", "test", "test");
			
			server.createMRecord("test", "Gest", "tset", "test", "test", "test");
			
			server.printAllRecords();
			
			server.editRecord("Xest", "CR10003", "newStatus");
			
			server.printAllRecords();
			
			server.getRecordCounts();
		}catch(Exception e){
			
		}
		
	}
	
	private static StationServerInterface loadServer(String serverConnectionString){
		StationServerInterface server = null;
		try{
			System.setSecurityManager(new RMISecurityManager());
			server = (StationServerInterface) Naming.lookup("rmi://" + serverConnectionString);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return server;
	}
	
	private static void displayWelcome(){
		System.out.println("Welcome to Distributed Police Information System (DPIS)");
	}
	
	private static String getBadgeID(){
		System.out.println();
		System.out.print("Please enter your badge ID:");
		
		Scanner keyboard = new Scanner(System.in);
		String userInput = keyboard.next();
		
		return userInput;
		
	}
	
	private static String extractStationNameFromBadgeID(String badgeID){
		int i = 0;
		while (!Character.isDigit(badgeID.charAt(i))){
			i++;
		}
		
		return badgeID.substring(0,i);
	}

}
